package com.xiaoshu.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 功能说明：客户端鉴权拦截器，拦截客户端的请求信息
 * com.xiaoshu.interceptor
 * 这个地方可以对客户请求进行鉴权处理，如果鉴权不通过，则返回false； 后续操作不在继续；
 * <p>
 * Original @Author: deane.jia-贾亮亮,2021/6/17 14:23
 * <p>
 * Copyright (C)2012-2021 小树盛凯科技 All rights reserved.
 */
@Component
@Order(0)
@Slf4j
public class ClientAuthorizationInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        if (!(handler instanceof HandlerMethod)) {
            return true;
        } else {
            HandlerMethod method = (HandlerMethod)handler;
            if (method.getBeanType().getAnnotation(ClientAuthorization.class) == null && method.getMethodAnnotation(ClientAuthorization.class) == null) {
                return true;
            }else {
                // TODO  此处判断头部Token信息，如果鉴权通过，则返回true；否则返回false；
                String requestMethod = request.getMethod();
                log.info("[Client Interceptor] method name is :{} , If the authentication fails, return false", requestMethod);
                return true;
            }
        }
    }
}
